home *** CD-ROM | disk | FTP | other *** search
Text File | 2000-06-23 | 53.6 KB | 2,026 lines |
- //////////
- //
- // File: MacFramework.c
- //
- // Contains: Basic support for playing QuickTime and QuickTime VR movies in a Macintosh application.
- // MacFramework is a simple QuickTime viewer and editor framework. It handles windows,
- // menus, events, and other low-level things. Put your application-specific code into the
- // file ComApplication.c.
- //
- // Written by: Tim Monroe
- // Based on the MovieShell code written by Apple DTS (Kent Sandvik).
- //
- // Copyright: © 1994-1999 by Apple Computer, Inc., all rights reserved.
- //
- // Change History (most recent first):GetFrontWindow
- //
- // <52> 06/07/99 rtm added GetFrontAppWindow and GetNextAppWindow; revised existing GetFrontMovieWindow
- // and GetNextMovieWindow
- // <51> 06/05/99 rtm added gAppFSSpec to keep track of the application's location
- // <50> 05/12/99 rtm tweaked DoSaveAsMovieWindow to ensure it creates single-fork movies; renamed it
- // as DoSaveAsMovieFile
- // <49> 05/11/99 rtm further work on DoSaveAsMovieWindow
- // <48> 05/10/99 rtm commented CloseMovieFile out of DoCreateMovieWindow; tweaked DoUpdateMovieFile
- // and DoSaveAsMovieWindow to reflect changes
- // <47> 05/06/99 rtm added createMovieFileDontCreateResFile flag to FlattenMovieData call
- // in DoSaveAsMovieWindow
- // <46> 03/31/99 rtm added default name for new movies; added DoSaveAsMovieWindow to implement
- // correct "Save As" behavior
- // <45> 03/18/99 rtm added SetMoviePlayHints call in DoCreateMovieWindow to support possible
- // dynamic resizing of streamed movies
- // <44> 01/26/99 rtm added EnableItem call for iSaveAs menu item in AdjustMenus
- // <43> 12/09/98 rtm modified inDrag case in MainEventLoop to handle non-movie windows too
- // <42> 10/27/98 rtm reworked DoCreateMovieWindow to take movie name from the FSSpec when
- // a movie is passed in
- // <41> 10/20/98 rtm added default progress procedure for all movies (in DoCreateMovieWindow)
- // <40> 10/15/98 rtm added autostarting for streamed movies
- // <39> 05/06/98 rtm added CheckQuickTimeRegistration call to main
- // <38> 04/16/98 rtm added EventAvail loop to InitMacEnvironment
- // <37> 02/13/98 rtm added GetWindowWidth; reworked SizeWindowToMovie to call it
- // <36> 02/09/98 rtm added GetWindowReferenceFromPort
- // <35> 02/02/98 rtm added SetMenuState
- // <34> 01/06/98 rtm added DoActivateWindow call to ShowAboutBox
- // <33> 12/22/97 rtm added AdjustMenus call to HandleKeyPress, as prescribed by Inside Macintosh:
- // Macintosh Toolbox Essentials, p. 3-78
- // <32> 12/18/97 rtm moved InitApplication call from InitMacEnvironment to main (to follow EnterMovies)
- // <31> 12/15/97 rtm added mouseDown case to StandardModalDialogEventFilter
- // <30> 12/12/97 rtm reworked DoCreateMovieWindow
- // <29> 12/08/97 rtm finished support for Save in DoDestroyMovieWindow; moved main to this file
- // <28> 12/05/97 rtm added fDirty flag to window object; begun supporting Save and Save As
- // <27> 12/01/97 rtm added gAppResFile global variable; see ShowAboutBox for details
- // <26> 11/07/97 rtm revised call to DoApplicationEventLoopAction
- // <25> 10/31/97 rtm fixed crashing bug in DoDestroyMovieWindow; see note there
- // <24> 10/30/97 rtm added GetPortFromWindowReference
- // <23> 09/18/97 rtm added GetFrontWindow and GetNextWindow
- // <22> 09/11/97 rtm added SizeWindowToMovie
- // <21> 07/23/97 rtm added GetWindowObjectFromWindow; rewrote GetWindowObjectFromFrontWindow
- // and all other functions that previously called GetWRefCon directly
- // <20> 07/18/97 rtm added calls to new Dialog Manager standard filter proc if it's available
- // <19> 07/17/97 rtm used new Dialog Manager constants for ok and cancel;
- // added QuitFramework (called from iQuit menu and from 'quit' Apple event)
- // <18> 07/15/97 rtm revised GetMCFromFrontWindow to use GetWindowObjectFromFrontWindow;
- // added AdjustMenus to InitMenuBar
- // <17> 07/11/97 rtm changed event loop to call AdjustMenus only before MenuSelect;
- // added DoApplicationEventLoopAction
- // <16> 06/12/97 rtm added standard modal dialog event filter and standard user item procedure
- // <15> 06/10/97 rtm replaced existing grow zone procedure with a better one
- // <14> 06/05/97 rtm added fCanResizeWindow to app data record, replacing an app global
- // <13> 02/26/97 rtm reworked AdjustMenus slightly; see <10> also
- // <12> 02/17/97 rtm reworked DoDestroyMovieWindow
- // <11> 02/04/97 rtm tweaked gLimitRect to fix bug #1624600
- // <10> 02/03/97 rtm Select All edit menu item now disabled for QTVR movies
- // <9> 01/06/97 rtm DoIdle now called for all open app windows
- // <8> 01/02/97 rtm added gAppInForeground flag
- // <7> 12/17/96 rtm fixed crashing bug in DoCreateMovieWindow
- // (hitting Cancel in open dialog left wrong port setting)
- // <6> 12/17/96 rtm added GetAppDataFromFrontWindow, GetAppDataFromWindow, GetAppDataFromWindowObject
- // <5> 12/09/96 rtm added RemoveApplicationWindowObject hook to allow app-specific clean-up
- // <4> 12/04/96 rtm added support for high-level events
- // <3> 12/02/96 rtm added CheckMovieControllers function
- // <2> 11/27/96 rtm conversion to personal coding style
- // <1> 12/20/94 khs first file
- //
- //////////
-
- // application header files
- #include "MacFramework.h"
- #include "AppConfiguration.h"
-
- // global variables
- Rect gDefaultWindowRect;
- Rect gLimitRect = {10, 10, 480, 640}; // max size for any window
- long gMCFlags = kMCFlags;
-
- Boolean gQuitFlag = false; // flag that keeps track of termination state
- Boolean gAppInForeground; // is our application in the foreground?
- Boolean gHasNewDialogCalls; // are the new Dialog Manager functions available?
-
- unsigned long gWNEsleep = kWNEDefaultSleep; // WaitNextEvent sleep time
- Str255 gWindowTitle = kDefaultWindowTitle; // default name for created windows
- GrowZoneUPP gAppGrowZoneUPP; // UPP to our grow zone callback
- ModalFilterUPP gModalFilterUPP; // UPP to our custom dialog event filter
- UserItemUPP gUserItemProcUPP; // UPP to our custom dialog user item procedure
- Handle gEmergencyMemory; // handle to our emergency memory reserve
-
- short gAppResFile = kInvalidFileRefNum; // file reference number for this application's resource file
- FSSpec gAppFSSpec; // file specification for the application itself
- Str255 gAppName; // the name of this application
-
-
- //////////
- //
- // main
- // Set up the application's execution environment; make sure QuickTime (etc.) is installed.
- //
- //////////
-
- void main (void)
- {
- OSErr myErr = noErr;
-
- InitMacEnvironment(10L); // 10 * MoreMasters
-
- if (!QTUtils_IsQuickTimeInstalled()) {
- ShowWarning("\pThe QuickTime extension is not present in this system (exit)", 0);
- ExitToShell();
- }
-
- #if TARGET_CPU_PPC
- if (!QTUtils_IsQuickTimeCFMInstalled()) {
- ShowWarning("\pThe QuickTime PowerPlug extension is not available (exit)", 0);
- ExitToShell();
- }
- #endif
-
- myErr = EnterMovies();
- if (myErr != noErr) {
- ShowWarning("\pCould not initialize the QuickTime environment (exit): ", myErr);
- ExitToShell();
- }
-
- // do any application-specific initialization
- InitApplication(kInitAppPhase_BothPhases);
-
- MainEventLoop(); // get and process commands until user quits
-
- ExitMovies();
-
- ExitToShell();
- }
-
-
- //////////
- //
- // InitMacEnvironment
- // Initialize the Macintosh runtime environment.
- //
- //////////
-
- void InitMacEnvironment (long theNumMasters)
- {
- long myIndex;
- EventRecord myEvent;
- short myVRefNum;
- long myDirID;
- OSErr myErr;
-
- // add more space to the stack
- InitStack(kExtraStackSpaceSize);
-
- // expand heap zone to its limit
- MaxApplZone();
-
- // allocate master pointer blocks
- for (myIndex = 0; myIndex < theNumMasters; myIndex++)
- MoreMasters();
-
- InitGraf(&qd.thePort);
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(NULL);
- InitCursor();
-
- // pull the application into the foreground; until an application has made a few calls
- // to the Event Manager, MultiFinder keeps it in the background, and splash screens and
- // alert boxes will remain in a background layer (this is documented in Tech Note #180)
- for (myIndex = 1; myIndex <= kBroughtToFront; myIndex++)
- myErr = EventAvail(everyEvent, &myEvent);
-
- // initialize and install the menu bar
- InitMenubar();
-
- // install a grow zone procedure to handle low memory situations
- gEmergencyMemory = NewHandle(kEmergencyMemorySize);
- if (gEmergencyMemory != NULL) {
- gAppGrowZoneUPP = NewGrowZoneProc(AppGrowZoneCallback);
- SetGrowZone(gAppGrowZoneUPP);
- }
-
- // initialize foreground/background state
- gAppInForeground = true;
-
- // see whether the new Dialog Manager functions are available
- gHasNewDialogCalls = QTUtils_TrapAvailable(_DialogDispatch);
-
- // create modal dialog filter and user item UPPs
- gModalFilterUPP = NewModalFilterProc(StandardModalDialogEventFilter);
- gUserItemProcUPP = NewUserItemProc(StandardUserItemProcedure);
-
- // get the application's resource file
- gAppResFile = CurResFile();
-
- // get the application's name from the resource file
- GetIndString(gAppName, kAppNameResID, kAppNameResIndex);
-
- // get the application's location and save it in gAppFSSpec
- HGetVol(NULL, &myVRefNum, &myDirID);
- FSMakeFSSpec(myVRefNum, myDirID, gAppName, &gAppFSSpec);
- }
-
-
- //////////
- //
- // InitStack
- // Add some extra space to the stack.
- //
- //////////
-
- void InitStack (long theExtraStackSpace)
- {
- Ptr mySize;
-
- mySize = GetApplLimit();
- SetApplLimit((Ptr)(mySize - theExtraStackSpace));
- }
-
-
- //////////
- //
- // AppGrowZoneCallback
- // A grow zone procedure. This is straight out of IM: Memory (pp. 1-46ff)
- //
- //////////
-
- PASCAL_RTN long AppGrowZoneCallback (Size theBytesNeeded)
- {
- #pragma unused(theBytesNeeded)
-
- long myA5;
- Size myBytesFreed;
-
- // get current A5; we might get called at a time that A5 isn't valid
- myA5 = SetCurrentA5();
-
- if ((*gEmergencyMemory != NULL) && (gEmergencyMemory != GZSaveHnd())) {
- EmptyHandle(gEmergencyMemory);
- myBytesFreed = kEmergencyMemorySize;
- } else {
- myBytesFreed = 0; // no more memory to release
- }
-
- myA5 = SetA5(myA5);
-
- return(myBytesFreed);
- }
-
-
- //////////
- //
- // InitMenubar
- // Set up the menu bar. This is straight out of IM: Macintosh Toolbox Essentials (pp. 3-50ff)
- //
- //////////
-
- Boolean InitMenubar (void)
- {
- Handle myMenuHandle = NULL;
-
- myMenuHandle = GetNewMBar(mMenubar); // read the resource-defined menu bar
- if (myMenuHandle == NULL)
- return(false);
-
- SetMenuBar(myMenuHandle); // install the menus
- DisposeHandle(myMenuHandle);
- AppendResMenu(GetMenuHandle(mApple), 'DRVR'); // add desk accessory names to Apple menu
- AdjustMenus();
- DrawMenuBar();
-
- return(true);
- }
-
-
- //////////
- //
- // MainEventLoop
- // Retrieve and process events.
- //
- //////////
-
- void MainEventLoop (void)
- {
- EventRecord myEvent;
- WindowRef myWindow;
- Boolean isEventHandled;
- short myWindowPart;
- Rect myScreenRect;
- Rect myRefreshArea;
-
- while (!gQuitFlag) {
-
- // make sure we've still got our memory reserve; reallocate it if it's been used
- if ((gEmergencyMemory == NULL) || (*gEmergencyMemory == NULL))
- ReallocateHandle(gEmergencyMemory, kEmergencyMemorySize);
-
- WaitNextEvent(everyEvent, &myEvent, gWNEsleep, NULL);
-
- // first, perform any application-specific event loop actions
- isEventHandled = DoApplicationEventLoopAction(&myEvent);
-
- // then, let all active movie controllers have access to the event
- if (!isEventHandled)
- isEventHandled = CheckMovieControllers(&myEvent);
-
- // then, if this event hasn't been handled, pass it on to the case statement
- // that dispatches the event to the right function
- if (!isEventHandled) {
-
- myWindow = FrontWindow();
-
- switch (myEvent.what) {
- case mouseDown:
-
- myWindowPart = FindWindow(myEvent.where, &myWindow);
-
- // menu bar and window-related events:
- switch (myWindowPart) {
- case inSysWindow:
- // a mouse click in a window belonging to a desk accessory
- SystemClick(&myEvent, myWindow);
- break;
-
- case inMenuBar:
- // a mouse click in the menu bar
- AdjustMenus();
- HandleMenuCommand(MenuSelect(myEvent.where));
- break;
-
- case inDrag: {
- WindowObject myWindowObject = NULL;
- MovieController myMC = NULL;
- Movie myMovie = NULL;
- Rect myRect;
-
- myWindowObject = GetWindowObjectFromWindow(myWindow);
- if (myWindowObject != NULL) {
- myMC = (**myWindowObject).fController;
- if (myMC != NULL) {
- myMovie = (**myWindowObject).fMovie;
- if (myMovie != NULL) {
- GetMovieBox(myMovie, &myRect);
- myScreenRect = (**GetGrayRgn()).rgnBBox;
- DragAlignedWindow(myWindow, myEvent.where, &myScreenRect, &myRect, NULL);
- }
- }
- } else {
- myScreenRect = (**GetGrayRgn()).rgnBBox;
- DragWindow(myWindow, myEvent.where, &myScreenRect);
- }
-
- break;
- }
-
- case inContent:
- if (myWindow != FrontWindow()) {
- SelectWindow(myWindow);
- MacSetPort(myWindow);
- } else {
- HandleContentClick(myWindow, &myEvent);
- }
-
- break;
-
- case inGoAway:
- // if the window is closed, dispose the movie, the controller, and the window
- if (TrackGoAway(myWindow, myEvent.where))
- DoDestroyMovieWindow(myWindow);
- break;
- } // end switch(myWindowPart)
- break;
-
- // system-level events:
- case updateEvt:
- myWindow = (WindowReference)myEvent.message;
- if (myWindow != NULL) {
- myRefreshArea = ((**(myWindow->visRgn)).rgnBBox);
- DoUpdateWindow(myWindow, &myRefreshArea);
- }
- break;
-
- case keyDown:
- case autoKey:
- HandleKeyPress(&myEvent);
- break;
-
- case diskEvt: {
- Point myPoint = {100, 100};
-
- if (HiWord(myEvent.message) != noErr)
- (void)DIBadMount(myPoint, myEvent.message);
- break;
- }
-
- case activateEvt:
- myWindow = (WindowReference)myEvent.message;
-
- if (IsAppWindow(myWindow)) {
- DoActivateWindow(myWindow, ((myEvent.modifiers & activeFlag) != 0 ));
- }
- break;
-
- case osEvt:
- switch ((myEvent.message >> 24) & 0x00FF) { // get high byte of message
- case suspendResumeMessage:
-
- // set the foreground/background state
- if ((myEvent.message & resumeFlag) != 0)
- gAppInForeground = true;
- else
- gAppInForeground = false;
-
- // activate the front window, if there is one
- myWindow = FrontWindow();
- if (myWindow != NULL)
- DoActivateWindow(myWindow, !((myEvent.message & resumeFlag) == 0));
- break;
-
- case mouseMovedMessage:
- break;
- }
- break;
-
- case kHighLevelEvent:
- AEProcessAppleEvent(&myEvent);
- break;
-
- case nullEvent:
- // do idle-time processing for all open windows in our window list
- myWindow = GetFrontMovieWindow();
- while (myWindow != NULL) {
- if (gAppInForeground)
- DoIdle(myWindow);
-
- myWindow = GetNextMovieWindow(myWindow);
- }
- break;
-
- } // switch (myEvent.what)
- } // if (!isMovieEvent)
- } // while (!gQuitFlag)
- }
-
-
- //////////
- //
- // HandleMenuCommand
- // Handle a menu selection.
- //
- //////////
-
- void HandleMenuCommand (long theMenuResult)
- {
- short myMenuID, myMenuItem;
- Str255 myDAName;
-
- MacSetCursor(&qd.arrow);
-
- myMenuID = HiWord(theMenuResult);
- myMenuItem = LoWord(theMenuResult);
-
- switch (myMenuID) {
-
- case mApple:
- switch (myMenuItem) {
- case iAbout: // about box
- ShowAboutBox();
- break;
-
- default: // Apple menu handling
- GetMenuItemText(GetMenuHandle(mApple), myMenuItem, myDAName);
- (void)OpenDeskAcc(myDAName);
- break;
- }
- break;
-
- case mFile:
- switch (myMenuItem) {
- case iNew:
- DoCreateNewMovie();
- break;
-
- case iOpen:
- DoCreateMovieWindow(NULL, NULL);
- break;
-
- case iClose:
- DoDestroyMovieWindow(GetFrontAppWindow());
- break;
-
- case iSave:
- DoUpdateMovieFile(GetFrontMovieWindow());
- break;
-
- case iSaveAs:
- DoSaveAsMovieFile(GetFrontMovieWindow());
- break;
-
- case iPrint: {
- MovieController myMC;
- OSErr myErr = noErr;
-
- myMC = GetMCFromFrontWindow();
- if (myMC != NULL) {
- myErr = QTUtils_PrintMoviePICT(MCGetMovie(myMC), kDefaultX, kDefaultY, kPrintFrame);
- if (myErr != noErr) {
- ShowWarning("\pCould not print!", myErr);
- SysBeep(kDefaultSysBeep);
- }
- } else
- SysBeep(kDefaultSysBeep);
- break;
- }
-
- case iQuit:
- QuitFramework();
- break;
-
- }
- break;
-
- // provide the default controller cut, copy and paste functionality
- case mEdit: {
- Movie myMovie = NULL;
- MovieController myMC = NULL;
- WindowObject myWindowObject = NULL;
-
- myMC = GetMCFromFrontWindow();
- myWindowObject = GetWindowObjectFromFrontWindow();
-
- if (myMenuItem == iPreferences) {
- HandleApplicationMenu(MENU_IDENTIFIER(myMenuID, myMenuItem));
- break;
- }
-
- if ((myMC == NULL) || (myWindowObject == NULL))
- break;
-
- switch (myMenuItem) {
- case iUndo:
- MCUndo(myMC);
- (**myWindowObject).fDirty = true;
- break;
-
- case iCut:
- myMovie = MCCut(myMC);
- (**myWindowObject).fDirty = true;
- break;
-
- case iCopy:
- myMovie = MCCopy(myMC);
- break;
-
- case iPaste:
- MCPaste(myMC, NULL);
- (**myWindowObject).fDirty = true;
- break;
-
- case iClear:
- MCClear(myMC);
- (**myWindowObject).fDirty = true;
- break;
-
- case iSelectAll:
- if (QTUtils_SelectAllMovie(myMC) != noErr)
- SysBeep(kDefaultSysBeep);
- break;
- }
-
- if (myMovie) {
- PutMovieOnScrap(myMovie, 0L);
- DisposeMovie(myMovie);
- }
-
- break;
- }
-
-
- default:
- // do any application-specific menu handling
- HandleApplicationMenu(MENU_IDENTIFIER(myMenuID, myMenuItem));
- break;
-
- } // switch (myMenuID)
-
- HiliteMenu(0);
- }
-
-
- //////////
- //
- // AdjustMenus
- // Adjust the application's File and Edit menus.
- //
- //////////
-
- void AdjustMenus (void)
- {
- WindowRef myWindow = NULL;
- WindowObject myWindowObject = NULL;
- MovieController myMC = NULL;
- MenuHandle myMenu;
-
- myWindow = FrontWindow();
-
- if (myWindow != NULL)
- myWindowObject = GetWindowObjectFromWindow(myWindow);
-
- if (myWindowObject != NULL)
- myMC = (**myWindowObject).fController;
-
- if (myWindow != NULL) {
-
- // handle the File menu's Close command
- EnableItem(GetMenuHandle(mFile), iClose);
-
- if (myWindowObject != NULL) {
-
- // handle the File menu's Save As and Print commands
- EnableItem(GetMenuHandle(mFile), iSaveAs);
- EnableItem(GetMenuHandle(mFile), iPrint);
-
- // let the movie controller do the right thing for the Edit menu
- if (myMC != NULL) {
- long myFlags;
-
- MCSetUpEditMenu(myMC, 0L, GetMenuHandle(mEdit));
-
- // MCSetUpEditMenu doesn't handle Select All, so we'll do it ourselves....
- MCGetControllerInfo(myMC, &myFlags);
- if (myFlags & mcInfoEditingEnabled)
- EnableItem(GetMenuHandle(mEdit), iSelectAll);
- else
- DisableItem(GetMenuHandle(mEdit), iSelectAll);
- }
-
- // handle the File menu's Save menu command
- if ((**myWindowObject).fDirty)
- EnableItem(GetMenuHandle(mFile), iSave);
- else
- DisableItem(GetMenuHandle(mFile), iSave);
- } else {
- // handle the File menu's Save As and Print commands
- DisableItem(GetMenuHandle(mFile), iSaveAs);
- DisableItem(GetMenuHandle(mFile), iPrint);
- }
-
- } else {
- // handle the File menu
- myMenu = GetMenuHandle(mFile);
- DisableItem(myMenu, iSave);
- DisableItem(myMenu, iSaveAs);
- DisableItem(myMenu, iClose);
- DisableItem(myMenu, iPrint);
-
- // handle the Edit menu
- myMenu = GetMenuHandle(mEdit);
- DisableItem(myMenu, iCut);
- DisableItem(myMenu, iCopy);
- DisableItem(myMenu, iPaste);
- DisableItem(myMenu, iUndo);
- DisableItem(myMenu, iClear);
- DisableItem(myMenu, iSelectAll);
- }
-
- AdjustApplicationMenus(myWindow, NULL); // adjust any app-specific menus as well
- }
-
-
- //////////
- //
- // HandleKeyPress
- // Handle key presses. This is modelled on Inside Macintosh: Macintosh Toolbox Essentials, p. 3-78.
- //
- //////////
-
- void HandleKeyPress (EventRecord *theEvent)
- {
- char myKey;
-
- myKey = theEvent->message & charCodeMask;
-
- if (theEvent->modifiers & cmdKey) {
- // if the command key is down, it must be a keyboard shortcut for a menu selection;
- // adjust the menus and find the menu command that the shortcut picks out
- AdjustMenus();
- HandleMenuCommand(MenuKey(myKey));
- } else {
- // otherwise, we'll assume it's meant for our application
- HandleApplicationKeyPress(myKey);
- }
- }
-
-
- //////////
- //
- // QuitFramework
- // Do any framework-specific shut-down.
- //
- //////////
-
- void QuitFramework (void)
- {
- WindowReference myWindow;
- WindowReference myNextWindow;
-
- // set quit flag, so we exit the event loop the next time thru
- gQuitFlag = true;
-
- // do any application-specific shutdown: before destroying any open windows
- StopApplication(kStopAppPhase_BeforeDestroyWindows);
-
- // walk the window list and destroy any open windows
- myWindow = GetFrontAppWindow();
- while (myWindow != NULL) {
- myNextWindow = GetNextAppWindow(myWindow);
- DoDestroyMovieWindow(myWindow);
- myWindow = myNextWindow;
- }
-
- // test the quit flag; a call to DoDestroyMovieWindow may have reset it
- if (!gQuitFlag)
- return;
-
- // do any application-specific shutdown: after destroying any open windows
- StopApplication(kStopAppPhase_AfterDestroyWindows);
-
- // release the grow zone memory
- DisposeHandle(gEmergencyMemory);
- }
-
-
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////
- //
- // Framework utilities.
- //
- // The framework uses the following functions to create movies and handle dialog boxes. You probably won't
- // need to use them directly.
- //
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////
-
- //////////
- //
- // DoCreateNewMovie
- // Create a new movie in a window; returns true if successful.
- //
- // NOTE: There are several user interface issues that are blissfully ignored by this routine,
- // principally the preferred names and the on-screen locations of the new windows.
- //
- //////////
-
- Boolean DoCreateNewMovie (void)
- {
- Movie myMovie = NULL;
- FSSpec myFSSpec;
- StringPtr myName = QTUtils_ConvertCToPascalString(kNewMovieName);
-
- myMovie = NewMovie(newMovieActive);
- if (myMovie == NULL)
- return(false);
-
- // create a default FSSpec
- FSMakeFSSpec(0, 0L, myName, &myFSSpec);
-
- free(myName);
-
- return(DoCreateMovieWindow(myMovie, &myFSSpec));
- }
-
-
- //////////
- //
- // SetupMovieWindowWithController
- // Configure the movie controller.
- //
- //////////
-
- MovieController SetupMovieWindowWithController (Movie theMovie, WindowReference theWindow, Boolean theMoveWindow)
- {
- MovieController myMC;
- Rect myRect;
- WindowObject myWindowObject;
- GrafPtr mySavedPort;
- short myMovieWidth;
- short myMovieHeight;
-
- myWindowObject = GetWindowObjectFromWindow(theWindow); // get our window specific data
-
- GetPort(&mySavedPort);
- MacSetPort((GrafPtr)theWindow);
-
- // resize the movie bounding rect and offset to 0,0
- GetMovieBox(theMovie, &myRect);
- MacOffsetRect(&myRect, -myRect.left, -myRect.top);
- SetMovieBox(theMovie, &myRect);
- AlignWindow((WindowPtr)theWindow, false, &myRect, NULL);
-
- // create the movie controller
- myMC = NewMovieController(theMovie, &myRect, gMCFlags);
- if (myMC == NULL)
- return(NULL);
-
- // enable the default movie controller editing
- MCEnableEditing(myMC, true);
-
- // suppress movie badge
- MCDoAction(myMC, mcActionSetUseBadge, (void *)false);
-
- MCGetControllerBoundsRect(myMC, &myRect);
-
- // add grow box for the movie controller
- MCDoAction(myMC, mcActionSetGrowBoxBounds, &gLimitRect);
-
- // set the initial looping state of the movie; this is a nice touch
- QTUtils_SetLoopingStateFromFile(theMovie, myMC);
-
- // install an action filter that does any application-specific mc action processing
- MCSetActionFilterWithRefCon(myMC, NewMCActionFilterWithRefConProc(ApplicationMCActionFilterProc), (long)myWindowObject);
-
- // see if the bounding rects are sane
- myMovieWidth = myRect.right - myRect.left;
- myMovieHeight = myRect.bottom - myRect.top;
-
- // resize the window
- SizeWindow(theWindow, myMovieWidth, myMovieHeight, true);
- if (theMoveWindow)
- MoveWindow(theWindow, kDefaultX, kDefaultY, false);
-
- MacSetPort(mySavedPort);
-
- // add any application-specific controller functionality
- AddControllerFunctionality(myMC);
-
- return(myMC);
- }
-
-
- //////////
- //
- // DoCreateMovieWindow
- // Open a movie in a new movie window; returns true if successful.
- //
- // This function is called from several places in our framework. The following combinations are possible:
- // * theMovie == NULL, theFSSpec == NULL: no movie, no file; elicit a movie file from user and open it
- // * theMovie != NULL, theFSSpec == NULL: new movie, no file (yet)
- // * theMovie == NULL, theFSSpec != NULL: no movie, but we have an FSSpec; so just open the specified movie file
- // * theMovie != NULL, theFSSpec != NULL: new movie, theFSSpec contains (at least) the movie name
- //
- //////////
-
- Boolean DoCreateMovieWindow (Movie theMovie, FSSpec *theFSSpec)
- {
- WindowObject myWindowObject = NULL;
- MovieController myMC = NULL;
- WindowReference myWindow = NULL;
- FSSpec myFSSpec;
- Movie myMovie = NULL;
- short myRefNum = kInvalidFileRefNum;
- short myResID = 0;
- SFTypeList myTypeList = {kQTFileTypeMovie, 0, 0, 0};
- StandardFileReply myReply;
- Rect myRect = gDefaultWindowRect;
- GrafPtr mySavedPort;
- OSErr myErr = noErr;
-
- // get the current port; we may need to restore it if we cannot successfully create a new window
- GetPort(&mySavedPort);
-
- // if we got neither a movie nor an FSSpec passed in, prompt the user for a movie file
- if ((theMovie == NULL) && (theFSSpec == NULL)) {
-
- StandardGetFilePreview(NULL, 1, myTypeList, &myReply);
- if (!myReply.sfGood)
- goto bail;
-
- // make an FSSpec record
- FSMakeFSSpec(myReply.sfFile.vRefNum, myReply.sfFile.parID, myReply.sfFile.name, &myFSSpec);
- }
-
- // if we got an FSSpec passed in, copy it into myFSSpec
- if (theFSSpec != NULL) {
-
- // make an FSSpec record
- FSMakeFSSpec(theFSSpec->vRefNum, theFSSpec->parID, theFSSpec->name, &myFSSpec);
- }
-
- // if we got no movie passed in, read one from the specified file
- if (theMovie == NULL) {
-
- // ideally, we'd like read and write permission, but we'll settle for read-only permission
- myErr = OpenMovieFile(&myFSSpec, &myRefNum, fsRdWrPerm);
- if (myErr != noErr)
- myErr = OpenMovieFile(&myFSSpec, &myRefNum, fsRdPerm);
-
- // if we couldn't open the file with even just read-only permission, bail....
- if (myErr != noErr)
- goto bail;
-
- // now fetch the first movie from the file
- myResID = 0;
- myErr = NewMovieFromFile(&myMovie, myRefNum, &myResID, NULL, newMovieActive, NULL);
- if (myErr != noErr)
- goto bail;
-
- // we will allow the user to edit our files, so we keep the movie file open
- // CloseMovieFile(myRefNum);
-
- } else {
- myMovie = theMovie;
- }
-
- //////////
- //
- // at this point, myMovie is an open movie, but myFSSpec may or may not be a valid FSSpec
- //
- //////////
-
- // set the default progress procedure for the movie
- SetMovieProgressProc(myMovie, (MovieProgressUPP)-1, 0);
-
- // create a new window to display the movie in
- myWindow = CreateMovieWindow(&myRect, gWindowTitle);
- if (myWindow == NULL)
- goto bail;
-
- MacSetPort(GetPortFromWindowReference(myWindow));
- myWindowObject = CreateWindowObject(myWindow);
- if (myWindowObject == NULL)
- goto bail;
-
- // set the window title
- SetWTitle(myWindow, myFSSpec.name);
-
- // make sure the movie uses the window GWorld in all situations
- SetMovieGWorld(myMovie, (CGrafPtr)GetPortFromWindowReference(myWindow), GetGWorldDevice((CGrafPtr)GetPortFromWindowReference(myWindow)));
-
- // create and configure the movie controller
- myMC = SetupMovieWindowWithController(myMovie, myWindow, true);
-
- // store movie info in the window record
- (**myWindowObject).fMovie = myMovie;
- (**myWindowObject).fController = myMC;
- (**myWindowObject).fFileResID = myResID;
- (**myWindowObject).fFileRefNum = myRefNum;
- (**myWindowObject).fCanResizeWindow = true;
- (**myWindowObject).fDirty = false;
- (**myWindowObject).fInstance = NULL;
- (**myWindowObject).fAppData = NULL;
- (**myWindowObject).fFileFSSpec = myFSSpec;
-
- // do any application-specific window object initialization
- InitApplicationWindowObject(myWindowObject);
-
- // size the window to fit the movie and controller
- SizeWindowToMovie(myWindowObject);
-
- // set the movie's play hints to allow dynamic resizing
- SetMoviePlayHints(myMovie, hintsAllowDynamicResize, hintsAllowDynamicResize);
-
- // show the window
- ShowWindow(myWindow);
- SelectWindow(myWindow); // make it front-most, since it's just been created
- InvalRect(&((GrafPtr)myWindow)->portRect);
-
- // if the movie is a streamed movie, then start it playing immediately
- if (QTUtils_IsStreamedMovie(myMovie))
- MCDoAction(myMC, mcActionPrerollAndPlay, (void *)GetMoviePreferredRate(myMovie));
-
- return(true);
-
- bail:
- if (myWindow != NULL)
- DisposeWindow(myWindow);
-
- if (myMC != NULL)
- DisposeMovieController(myMC);
-
- if (myMovie != NULL)
- DisposeMovie(myMovie);
-
- if (myRefNum != 0)
- CloseMovieFile(myRefNum);
-
- MacSetPort(mySavedPort); // restore the port that was active when this function was called
-
- return(false);
- }
-
-
- //////////
- //
- // DoDestroyMovieWindow
- // Close the specified movie window.
- //
- //////////
-
- void DoDestroyMovieWindow (WindowReference theWindow)
- {
- Movie myMovie = NULL;
- MovieController myMC = NULL;
- WindowObject myWindowObject = NULL;
-
- // get the window object associated with the specified window
- myWindowObject = GetWindowObjectFromWindow(theWindow);
- if (myWindowObject == NULL) {
- // if the window passed in isn't a movie window, just dispose of it and return
- if (theWindow != NULL) {
- DisposeWindow(theWindow);
- theWindow = NULL;
- }
- return;
- }
-
- // if the window's data is "dirty", give the user a chance to save it;
- // this is modeled on Inside Macintosh: Macintosh Toolbox Essentials, pp. 4-62f.
- if ((**myWindowObject).fDirty) {
- Str255 myString;
- short myItem;
- short mySavedResFile;
-
- // get the current resource file and set the application's resource file
- mySavedResFile = CurResFile();
- UseResFile(gAppResFile);
-
- // get the title of the window
- GetWTitle(theWindow, myString);
-
- // insert the application's name and the window title into the dialog box text
- ParamText(gAppName, myString, NULL, NULL);
-
- // display the "Save changes" dialog box
- myItem = CautionAlert(kSaveDialogID, gModalFilterUPP);
-
- UseResFile(mySavedResFile); // restore the original resource file
-
- switch (myItem) {
- case kSaveChanges:
- // save the data in the window
- DoUpdateMovieFile(theWindow);
- break;
-
- case kDontSaveChanges:
- // discard any unsaved changes (that is, don't do anything)
- break;
-
- case kCancelClose:
- // do not close the window, and do not quit the application
- gQuitFlag = false;
- return;
- }
- }
-
- myMC = (**myWindowObject).fController;
- myMovie = (**myWindowObject).fMovie;
-
- // remove our mcAction filter function
- MCSetActionFilterWithRefCon(myMC, NULL, NULL);
-
- // close the movie file
- if ((**myWindowObject).fFileRefNum != kInvalidFileRefNum) {
- CloseMovieFile((**myWindowObject).fFileRefNum);
- (**myWindowObject).fFileRefNum = kInvalidFileRefNum;
- }
-
- // order is important here; according to Inside Macintosh: QuickTime, p. 2-96:
- // "Do not dispose of a movie if it has any special clients--for example,
- // if it has an attached movie controller component.
- // Only dispose of the movie after any clients are done with it."
- if (myMC != NULL)
- DisposeMovieController(myMC);
-
- if (myMovie != NULL)
- DisposeMovie(myMovie);
-
- // do any application-specific window clean-up
- RemoveApplicationWindowObject(myWindowObject);
-
- DisposeHandle((Handle)myWindowObject);
- DisposeWindow(theWindow);
- }
-
-
- //////////
- //
- // DoSaveAsMovieFile
- // Save the movie in the specified window under a new name.
- //
- // Human interface guidelines for "Save As..." dictate that, if the user selects a new file name
- // for the current movie, then that new file shall become the active one. This means that we need
- // to close the current movie file and open the new one.
- //
- //////////
-
- OSErr DoSaveAsMovieFile (WindowReference theWindow)
- {
- WindowObject myWindowObject = NULL;
- Movie myMovie = NULL;
- StandardFileReply mySFReply;
- StringPtr myPrompt = QTUtils_ConvertCToPascalString(kSavePrompt);
- StringPtr myFileName = QTUtils_ConvertCToPascalString(kSaveMovieFileName);
- OSErr myErr = paramErr;
-
- // get the window object associated with the specified window
- myWindowObject = GetWindowObjectFromWindow(theWindow);
- if (myWindowObject == NULL)
- goto bail;
-
- myMovie = (**myWindowObject).fMovie;
- if (myMovie == NULL)
- goto bail;
-
- StandardPutFile(myPrompt, myFileName, &mySFReply);
- if (mySFReply.sfGood) {
- Movie myNewMovie = NULL;
- MovieController myMC = NULL;
- long myFlags;
- short myRefNum = kInvalidFileRefNum;
- short myResID = movieInDataForkResID;
-
- //////////
- //
- // we have a valid FSSpec for the new movie file; now we want to create a new movie file,
- // save the movie data into the new file, close the existing movie file, and then swap
- // the window object data
- //
- //////////
-
- // delete any existing file of that name
- if (mySFReply.sfReplacing) {
- myErr = DeleteMovieFile(&mySFReply.sfFile);
- if (myErr != noErr)
- goto bail;
- }
-
- myFlags = createMovieFileDeleteCurFile | createMovieFileDontOpenFile | createMovieFileDontCreateMovie | createMovieFileDontCreateResFile;
- myErr = CreateMovieFile(&mySFReply.sfFile, FOUR_CHAR_CODE('TVOD'), smSystemScript, myFlags, NULL, NULL);
- if (myErr != noErr)
- goto bail;
-
- myErr = OpenMovieFile(&mySFReply.sfFile, &myRefNum, fsRdWrPerm);
- if (myErr != noErr)
- goto bail;
-
- // write existing movie's data into new movie file
- myErr = AddMovieResource(myMovie, myRefNum, &myResID, mySFReply.sfFile.name);
- if (myErr != noErr)
- goto bail;
-
- // get the new movie from the file
- myErr = NewMovieFromFile(&myNewMovie, myRefNum, &myResID, NULL, newMovieActive, NULL);
- if (myErr != noErr)
- goto bail;
-
- // create a new movie controller
- myMC = SetupMovieWindowWithController(myNewMovie, theWindow, false);
-
- //////////
- //
- // if we got to here, we've successfully created a new movie file, and NewMovieFromFile has
- // returned the new movie to us; so we need to close down the current movie and install the
- // new movie in its place
- //
- //////////
-
- // close the existing movie file
- if ((**myWindowObject).fFileRefNum != kInvalidFileRefNum)
- CloseMovieFile((**myWindowObject).fFileRefNum);
-
- // dispose of the existing movie controller and movie resource
- DisposeMovieController((**myWindowObject).fController);
- DisposeMovie(myMovie);
-
- // keep track of the new info
- (**myWindowObject).fMovie = myNewMovie;
- (**myWindowObject).fController = myMC;
- (**myWindowObject).fFileFSSpec = mySFReply.sfFile;
- (**myWindowObject).fFileResID = myResID;
- (**myWindowObject).fFileRefNum = myRefNum;
- (**myWindowObject).fDirty = false;
-
- // make sure the movie uses the window GWorld in all situations
- SetMovieGWorld(myNewMovie, (CGrafPtr)GetPortFromWindowReference((**myWindowObject).fWindow), GetGWorldDevice((CGrafPtr)GetPortFromWindowReference((**myWindowObject).fWindow)));
-
- // set the window title
- SetWTitle(theWindow, mySFReply.sfFile.name);
- } else {
- myErr = userCanceledErr;
- }
-
- bail:
- free(myPrompt);
- free(myFileName);
-
- return(myErr);
- }
-
-
- //////////
- //
- // DoUpdateMovieFile
- // Update the file (if any) attached to the movie.
- //
- //////////
-
- Boolean DoUpdateMovieFile (WindowReference theWindow)
- {
- WindowObject myWindowObject = NULL;
- Movie myMovie = NULL;
- OSErr myErr = noErr;
-
- // get the window object associated with the specified window
- myWindowObject = GetWindowObjectFromWindow(theWindow);
- if (myWindowObject == NULL)
- return(false);
-
- myMovie = (**myWindowObject).fMovie;
- if (myMovie == NULL)
- return(false);
-
- // update the current volume setting
- QTUtils_UpdateMovieVolumeSetting(myMovie);
-
- if ((**myWindowObject).fFileRefNum == kInvalidFileRefNum) // brand new movie, so no file attached to it
- myErr = DoSaveAsMovieFile(theWindow);
- else // we have an existing file; just update the movie resource
- myErr = UpdateMovieResource(myMovie, (**myWindowObject).fFileRefNum, (**myWindowObject).fFileResID, NULL);
-
- (**myWindowObject).fDirty = false;
-
- return(myErr == noErr);
- }
-
-
- //////////
- //
- // DoActivateWindow
- // Activate or deactivate the specified window.
- //
- //////////
-
- void DoActivateWindow (WindowReference theWindow, Boolean isBecomingActive)
- {
- WindowObject myWindowObject = NULL;
- MovieController myMC = NULL;
- GrafPtr mySavedPort = NULL;
-
- if (theWindow == NULL)
- return;
-
- GetPort(&mySavedPort);
- MacSetPort((GrafPtr)theWindow);
-
- // get the window object associated with the specified window
- myWindowObject = GetWindowObjectFromWindow(theWindow);
- if (myWindowObject != NULL) {
- myMC = (**myWindowObject).fController;
- if (myMC != NULL)
- MCActivate(myMC, theWindow, isBecomingActive);
- }
-
- MacSetPort(mySavedPort);
- }
-
-
- //////////
- //
- // StandardUserItemProcedure
- // A standard user-item procedure to outline the OK button in a modal dialog.
- //
- //////////
-
- PASCAL_RTN void StandardUserItemProcedure (DialogPtr theDialog, short theItem)
- {
- #pragma unused(theItem)
-
- short myItemKind; // for GetDialogItem
- Handle myItemHandle; // for GetDialogItem
- Rect myItemRect; // for GetDialogItem
-
- if (!gHasNewDialogCalls) { // no need to do any of this if the new Dialog Manager calls are available
- GetDialogItem(theDialog, kStdOkItemIndex, &myItemKind, &myItemHandle, &myItemRect);
- MacInsetRect(&myItemRect, -4, -4);
- PenSize(3, 3);
- FrameRoundRect(&myItemRect, 16, 16);
- PenSize(1, 1);
- }
- }
-
-
- //////////
- //
- // StandardModalDialogEventFilter
- // A standard modal dialog event filter.
- //
- //////////
-
- PASCAL_RTN Boolean StandardModalDialogEventFilter (DialogPtr theDialog, EventRecord *theEvent, short *theItemHit)
- {
- Boolean myEventHandled = false;
- short myItemKind; // for GetDialogItem
- Handle myItemHandle; // for GetDialogItem
- Rect myItemRect; // for GetDialogItem
- unsigned long myTicks; // for Delay
- char myKey;
- WindowReference myWindow = NULL;
- ModalFilterUPP myStdModalProc; // the OS's standard filter procedure
- short myPart;
- OSErr myErr = noErr;
-
- switch (theEvent->what) {
- case updateEvt:
- // update the specified window, if it's behind the modal dialog
- myWindow = (WindowReference)theEvent->message;
- if ((myWindow != NULL) && (myWindow != theDialog)) {
- DoUpdateWindow(myWindow, &(**(myWindow->visRgn)).rgnBBox);
- myEventHandled = false; // so sayeth IM
- }
- break;
-
- case nullEvent:
- // do idle-time processing for all open windows in our window list
- myWindow = FrontWindow();
- while (myWindow != NULL) {
- if (gAppInForeground)
- DoIdle(myWindow);
-
- myWindow = (WindowPtr)(((WindowRecord*)myWindow)->nextWindow);
- }
-
- myEventHandled = false;
- break;
-
- case keyDown:
- case autoKey:
- // if new Dialog Manager calls are NOT available, handle certain key presses
- if (!gHasNewDialogCalls) {
- // first, map Command-period to Escape key...
- myKey = theEvent->message & charCodeMask;
- if (theEvent->modifiers & cmdKey)
- if (myKey == kPeriod)
- myKey = kEscapeKey;
-
- // ...then, handle the standard keyboard equivalents of OK and Cancel buttons
- switch (myKey) {
- case kReturnKey:
- case kEnterKey:
- *theItemHit = kStdOkItemIndex;
- GetDialogItem(theDialog, kStdOkItemIndex, &myItemKind, &myItemHandle, &myItemRect);
- HiliteControl((ControlHandle)myItemHandle, kControlButtonPart);
- Delay(kMyButtonDelay, &myTicks);
- HiliteControl((ControlHandle)myItemHandle, false);
- myEventHandled = true;
- break;
- case kEscapeKey:
- *theItemHit = kStdCancelItemIndex;
- GetDialogItem(theDialog, kStdCancelItemIndex, &myItemKind, &myItemHandle, &myItemRect);
- HiliteControl((ControlHandle)myItemHandle, kControlButtonPart);
- Delay(kMyButtonDelay, &myTicks);
- HiliteControl((ControlHandle)myItemHandle, false);
- myEventHandled = true;
- break;
- default:
- break;
- }
- }
- break;
-
- case mouseDown:
- myPart = FindWindow(theEvent->where, &myWindow);
- if (myPart == inDrag) {
- Rect myScreenRect;
-
- myScreenRect = (**GetGrayRgn()).rgnBBox;
- DragWindow(myWindow, theEvent->where, &myScreenRect);
- myEventHandled = true;
- }
- break;
-
- default:
- myEventHandled = false;
- break;
- }
-
- // let the OS's standard filter proc handle the event, if it hasn't already been handled
- if (gHasNewDialogCalls && (myEventHandled == false)) {
- myErr = GetStdFilterProc(&myStdModalProc);
- if (myErr == noErr)
- myEventHandled = CallModalFilterProc(myStdModalProc, theDialog, theEvent, theItemHit);
- }
-
- return(myEventHandled);
- }
-
-
- //////////
- //
- // CheckMovieControllers
- // Let all movie controllers have a chance to process the event.
- //
- // Returns true if the event was handled by some movie controller, false otherwise
- //
- //////////
-
- Boolean CheckMovieControllers (EventRecord *theEvent)
- {
- WindowPtr myWindow;
- WindowObject myWindowObject;
- MovieController myMC;
-
- myWindow = GetFrontMovieWindow();
- while (myWindow != NULL) {
- myWindowObject = GetWindowObjectFromWindow(myWindow);
- if (myWindowObject != NULL) {
- myMC = (**myWindowObject).fController;
- if (myMC != NULL)
- if (MCIsPlayerEvent(myMC, theEvent))
- return(true);
- }
-
- myWindow = GetNextMovieWindow(myWindow);
- }
-
- return(false);
- }
-
-
- //////////
- //
- // IsAppWindow
- // Does the specified window belong to our application?
- //
- //////////
-
- Boolean IsAppWindow (WindowReference theWindow)
- {
- short myWindowKind;
-
- if (theWindow == NULL) {
- return(false);
- } else {
- myWindowKind = ((WindowPeek)theWindow)->windowKind;
- return((myWindowKind >= userKind) || (myWindowKind == dialogKind));
- }
- }
-
-
- //////////
- //
- // IsDocWindow
- // Is the specified window a document window (having a WindowObject refcon)?
- //
- //////////
-
- Boolean IsDocWindow (WindowReference theWindow)
- {
- short myWindowKind;
-
- if (theWindow == NULL) {
- return(false);
- } else {
- myWindowKind = ((WindowPeek)theWindow)->windowKind;
- return(myWindowKind >= userKind);
- }
- }
-
-
- //////////
- //
- // CreateWindowObject
- // Create a window object for the specified window.
- //
- //////////
-
- WindowObject CreateWindowObject (WindowReference theWindow)
- {
- WindowObject myWindowObject = NULL;
-
- myWindowObject = (WindowObject)NewHandleClear(sizeof(WindowObjectRecord));
-
- if (myWindowObject != NULL) {
- (**myWindowObject).fWindow = theWindow;
- (**myWindowObject).fController = NULL;
- (**myWindowObject).fObjectType = kMovieControllerObject;
- (**myWindowObject).fInstance = NULL;
- (**myWindowObject).fDirty = false;
- (**myWindowObject).fAppData = NULL;
- }
-
- SetWRefCon(theWindow, (long)myWindowObject); // store a ref to the record in the window
-
- return(myWindowObject);
- }
-
-
- //////////
- //
- // SizeWindowToMovie
- // Set the window size to exactly fit the movie and controller (if visible).
- //
- //////////
-
- void SizeWindowToMovie (WindowObject theWindowObject)
- {
- Rect myMovieBounds;
- MovieController myMC = NULL;
- Movie myMovie = NULL;
-
- if (theWindowObject == NULL)
- return;
-
- myMC = (**theWindowObject).fController;
- myMovie = (**theWindowObject).fMovie;
-
- if (MCGetVisible(myMC))
- MCGetControllerBoundsRect(myMC, &myMovieBounds);
- else
- GetMovieBox(myMovie, &myMovieBounds);
-
- // make sure that the movie has a non-zero width;
- // a zero height is okay (for example, with a music movie with no controller bar)
- if (myMovieBounds.right - myMovieBounds.left == 0) {
- myMovieBounds.left = 0;
- myMovieBounds.right = GetWindowWidth((**theWindowObject).fWindow);
- }
-
- SizeWindow(GetPortFromWindowReference((**theWindowObject).fWindow),
- myMovieBounds.right - myMovieBounds.left,
- myMovieBounds.bottom - myMovieBounds.top,
- true);
- }
-
-
- //////////
- //
- // ShowAboutBox
- // Display and manage the About dialog box.
- //
- //////////
-
- void ShowAboutBox (void)
- {
- DialogPtr myDialog = NULL;
- WindowReference myWindow = NULL;
- short myItem;
- short mySavedResFile;
- GrafPtr mySavedPort;
- short myItemKind;
- Handle myItemHandle;
- Rect myItemRect;
-
- // get the current resource file and port
- mySavedResFile = CurResFile();
- GetPort(&mySavedPort);
-
- // set the application's resource file;
- // otherwise, we'd get the dialog's resources from the current resource file,
- // which might not be the correct one....
- UseResFile(gAppResFile);
-
- // deactivate any frontmost movie window
- myWindow = GetFrontMovieWindow();
- if (myWindow != NULL)
- DoActivateWindow(myWindow, false);
-
- myDialog = GetNewDialog(kAboutBoxID, NULL, (WindowPtr)-1L);
- if (myDialog == NULL)
- goto bail;
-
- MacSetPort((GrafPtr)myDialog);
-
- if (gHasNewDialogCalls)
- SetDialogDefaultItem(myDialog, kStdOkItemIndex);
-
- // make sure that the OK button is outlined in bold, even if new Dialog Manager calls not available
- GetDialogItem(myDialog, kOKButtonUserItem, &myItemKind, &myItemHandle, &myItemRect);
- SetDialogItem(myDialog, kOKButtonUserItem, myItemKind, (Handle)gUserItemProcUPP, &myItemRect);
-
- // display and handle events in the dialog box until the user clicks OK
- do {
- ModalDialog(gModalFilterUPP, &myItem);
- } while (myItem != kStdOkItemIndex);
-
- bail:
- // restore the previous resource file and port
- MacSetPort(mySavedPort);
- UseResFile(mySavedResFile);
-
- if (myDialog != NULL)
- DisposeDialog(myDialog);
- }
-
-
- //////////
- //
- // ShowWarning
- // Display a warning box.
- //
- //////////
-
- void ShowWarning (Str255 theMessage, OSErr theErr)
- {
- Str255 myString;
- short mySavedResFile;
-
- // get the current resource file and set the application's resource file
- mySavedResFile = CurResFile();
- UseResFile(gAppResFile);
-
- NumToString(theErr, myString);
- ParamText("\pWarning!", theMessage, theErr ? myString: NULL, NULL);
- Alert(kAlertErrorID, gModalFilterUPP);
-
- // restore the original resource file
- UseResFile(mySavedResFile);
- }
-
-
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////
- //
- // Window walking utilities.
- //
- // Use these two functions to iterate through all open movie windows belonging to the application.
- //
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////
-
- //////////
- //
- // GetFrontMovieWindow
- // Return a reference to the frontmost movie window.
- //
- //////////
-
- WindowReference GetFrontMovieWindow (void)
- {
- WindowReference myWindow = GetFrontAppWindow();
-
- while ((myWindow != NULL) && (GetWindowObjectFromWindow(myWindow) == NULL))
- myWindow = GetNextAppWindow(myWindow);
-
- return(myWindow);
- }
-
-
- //////////
- //
- // GetNextMovieWindow
- // Return a reference to the next movie window.
- //
- //////////
-
- WindowReference GetNextMovieWindow (WindowReference theWindow)
- {
- WindowReference myWindow = GetNextAppWindow(theWindow);
-
- while ((myWindow != NULL) && (GetWindowObjectFromWindow(myWindow) == NULL))
- myWindow = GetNextAppWindow(myWindow);
-
- return(myWindow);
- }
-
-
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////
- //
- // Data retrieval utilities.
- //
- // Use the following functions to retrieve the window object, the application-specific data, or the movie
- // controller that is associated with a window.
- //
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////
-
- //////////
- //
- // GetWindowObjectFromFrontWindow
- // Get the window object associated with the front window.
- //
- //////////
-
- WindowObject GetWindowObjectFromFrontWindow (void)
- {
- return(GetWindowObjectFromWindow(FrontWindow()));
- }
-
-
- //////////
- //
- // GetWindowObjectFromWindow
- // Get the window object associated with the specified window.
- //
- //////////
-
- WindowObject GetWindowObjectFromWindow (WindowReference theWindow)
- {
- WindowObject myWindowObject = NULL;
-
- if (!IsAppWindow(theWindow))
- return(NULL);
-
- myWindowObject = (WindowObject)GetWRefCon(theWindow);
-
- // make sure this is a window object
- if (!IsWindowObjectOurs(myWindowObject))
- return(NULL);
-
- return(myWindowObject);
- }
-
-
- //////////
- //
- // GetMCFromFrontWindow
- // Get the movie controller (if any) associated with the front window.
- //
- //////////
-
- MovieController GetMCFromFrontWindow (void)
- {
- MovieController myMC = NULL;
- WindowObject myWindowObject = NULL;
-
- myWindowObject = GetWindowObjectFromFrontWindow();
- if (myWindowObject != NULL)
- myMC = (**myWindowObject).fController;
-
- return(myMC);
- }
-
-
- //////////
- //
- // GetMCFromWindow
- // Get the movie controller (if any) associated with the specified window.
- //
- //////////
-
- MovieController GetMCFromWindow (WindowReference theWindow)
- {
- MovieController myMC = NULL;
- WindowObject myWindowObject = NULL;
-
- myWindowObject = GetWindowObjectFromWindow(theWindow);
- if (myWindowObject != NULL)
- myMC = (**myWindowObject).fController;
-
- return(myMC);
- }
-
-
- //////////
- //
- // GetQTVRInstanceFromFrontWindow
- // Get the QTVRInstance (if any) associated with the front window.
- //
- //////////
-
- QTVRInstance GetQTVRInstanceFromFrontWindow (void)
- {
- QTVRInstance myInstance = NULL;
- WindowObject myWindowObject = NULL;
-
- myWindowObject = GetWindowObjectFromFrontWindow();
- if (myWindowObject != NULL)
- myInstance = (**myWindowObject).fInstance;
-
- return(myInstance);
- }
-
-
- //////////
- //
- // GetAppDataFromFrontWindow
- // Get the application-specific data associated with the front window.
- //
- //////////
-
- Handle GetAppDataFromFrontWindow (void)
- {
- return(GetAppDataFromWindow(FrontWindow()));
- }
-
-
- //////////
- //
- // GetAppDataFromWindow
- // Get the application-specific data associated with the specified window.
- //
- //////////
-
- Handle GetAppDataFromWindow (WindowReference theWindow)
- {
- WindowObject myWindowObject = NULL;
-
- myWindowObject = GetWindowObjectFromWindow(theWindow);
- if (myWindowObject == NULL)
- return(NULL);
- else
- return(GetAppDataFromWindowObject(myWindowObject));
- }
-
-
- //////////
- //
- // GetAppDataFromWindowObject
- // Get the application-specific data associated with the specified window object.
- //
- //////////
-
- Handle GetAppDataFromWindowObject (WindowObject theWindowObject)
- {
- Handle myAppData = NULL;
-
- // make sure this is a window object belonging to our application
- if (!IsWindowObjectOurs(theWindowObject))
- return(myAppData);
-
- // get the app data handle from the window object
- myAppData = (**theWindowObject).fAppData;
-
- return(myAppData);
- }
-
-
- //////////
- //
- // IsWindowObjectOurs
- // Does the specified window object belong to our application?
- //
- //////////
-
- Boolean IsWindowObjectOurs (WindowObject theWindowObject)
- {
- OSType myType = NULL;
-
- if ((theWindowObject == NULL) || (*theWindowObject == NULL))
- return(false);
-
- myType = (**theWindowObject).fObjectType;
- if (myType == kMovieControllerObject)
- return(true);
- else
- return(false);
- }
-
-
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////
- //
- // Miscellaneous utilities.
- //
- // Use the following functions to play beeps, manipulate menus, and do other miscellaneous things. These
- // functions are intended for use in cross-platform code.
- //
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////
-
- //////////
- //
- // DoBeep
- // Beep.
- //
- //////////
-
- void DoBeep (void)
- {
- SysBeep(30);
- }
-
-
- //////////
- //
- // SetMenuState
- // Set the enabled/disabled state of a menu.
- //
- //////////
-
- void SetMenuState (MenuReference theMenu, UInt16 theMenuItem, short theState)
- {
- #pragma unused(theMenuItem)
- SetMenuItemState(theMenu, 0, theState); // menu item == 0 means the entire menu
- }
-
-
- //////////
- //
- // SetMenuItemState
- // Set the enabled/disabled state of a menu item.
- //
- //////////
-
- void SetMenuItemState (MenuReference theMenu, UInt16 theMenuItem, short theState)
- {
- if (theState == kEnableMenuItem)
- EnableItem(theMenu, MENU_ITEM(theMenuItem));
- else
- DisableItem(theMenu, MENU_ITEM(theMenuItem));
- }
-
-
- //////////
- //
- // SetMenuItemLabel
- // Set the label (that is, the text) of a menu item.
- //
- //////////
-
- void SetMenuItemLabel (MenuReference theMenu, UInt16 theMenuItem, char *theText)
- {
- Str255 myString;
- short mySIndex, myTIndex;
-
- // we need to remove the '&' character while converting to a Pascal string
- mySIndex = 1;
- for (myTIndex = 0; myTIndex < strlen(theText); myTIndex++) {
- if (theText[myTIndex] != '&') {
- myString[mySIndex] = theText[myTIndex];
- mySIndex++;
- }
- }
-
- myString[0] = mySIndex - 1;
-
- SetMenuItemText(theMenu, MENU_ITEM(theMenuItem), myString);
- }
-
-
- //////////
- //
- // SetMenuItemCheck
- // Set the check mark state state of a menu item.
- //
- //////////
-
- void SetMenuItemCheck (MenuReference theMenu, UInt16 theMenuItem, Boolean theState)
- {
- CheckItem(theMenu, MENU_ITEM(theMenuItem), theState);
- }
-
-
- //////////
- //
- // GetPortFromWindowReference
- // Return the graphics port associated with a window reference.
- //
- //////////
-
- GrafPtr GetPortFromWindowReference (WindowReference theWindow)
- {
- return((GrafPtr)theWindow);
- }
-
-
- //////////
- //
- // GetWindowReferenceFromPort
- // Return the window reference associated with a graphics port.
- //
- //////////
-
- WindowReference GetWindowReferenceFromPort (GrafPtr thePort)
- {
- return((WindowReference)thePort);
- }
-
-
- //////////
- //
- // GetWindowWidth
- // Return the width of the specified window.
- //
- //////////
-
- short GetWindowWidth (WindowReference theWindow)
- {
- short myWidth = 0;
-
- if (theWindow != NULL)
- myWidth = (theWindow)->portRect.right - (theWindow)->portRect.left;
-
- return(myWidth);
- }
-
-
- //////////
- //
- // GetFrontAppWindow
- // Return a reference to the frontmost window (whether or not it's a movie window).
- //
- //////////
-
- WindowReference GetFrontAppWindow (void)
- {
- return(FrontWindow());
- }
-
-
- //////////
- //
- // GetNextAppWindow
- // Return a reference to the next window (whether or not it's a movie window).
- //
- //////////
-
- WindowReference GetNextAppWindow (WindowReference theWindow)
- {
- if (theWindow != NULL)
- return((WindowPtr)(((WindowRecord*)theWindow)->nextWindow));
- else
- return(NULL);
- }
-